package com.example.gameapplication.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.gameapplication.R;
import com.example.gameapplication.adapter.RecordAdapter;
import com.example.gameapplication.bean.ScoreDataBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tencent.mmkv.MMKV;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class RecordActivity extends AppCompatActivity {

    @BindView(R.id.tv_back)
    FrameLayout tvBack;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    private Unbinder bind;
    private RecordAdapter recordAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        bind = ButterKnife.bind(this);
        init();
    }

    private void init() {
        rvList.setLayoutManager(new LinearLayoutManager(this));
        recordAdapter = new RecordAdapter(this);
        rvList.setAdapter(recordAdapter);
        List<ScoreDataBean> scoreDataBeans;
        MMKV mmkv = MMKV.defaultMMKV();
        String data = mmkv.decodeString("data");
        if (!TextUtils.isEmpty(data)) {
            scoreDataBeans = new Gson().fromJson(data, new TypeToken<List<ScoreDataBean>>() {
            }.getType());
        }else {
            scoreDataBeans=new ArrayList<>();
        }
        recordAdapter.setScoreDataBeans(scoreDataBeans);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bind!=null)bind.unbind();
    }

    @OnClick(R.id.tv_back)
    public void onViewClicked() {
        finish();
    }
}
